/*
 * \brief  Client-side rom session interface
 * \author Norman Feske
 * \date   2006-07-06
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__ROM_SESSION__CLIENT_H_
#define _INCLUDE__ROM_SESSION__CLIENT_H_

#include <rom_session/capability.h>
#include <base/ipc.h>
#include <base/lock.h>

namespace Genode {

	/*********************
	 ** Exception types **
	 *********************/

	class Rom_file_missing : public Exception { };


	class Rom_session_client : public Rom_session
	{
		private:

			Msgbuf<256> _snd_msg, _rcv_msg;
			Ipc_client  _ipc_client;
			Lock        _lock;

		public:

			/**
			 * Constructor
			 */
			Rom_session_client(Rom_session_capability session)
			: _ipc_client(session, &_snd_msg, &_rcv_msg) { }


			/***************************
			 ** Rom session interface **
			 ***************************/

			Rom_dataspace_capability dataspace()
			{
				Lock::Guard lock_guard(_lock);
				Rom_dataspace_capability ds;   /* default is an invalid capability */

				/* check if the rom_session_cap was invalid */
				if (_ipc_client.ready_for_send())
					_ipc_client << DATASPACE << IPC_CALL >> ds;

				if (!ds.valid())
					throw Rom_file_missing();

				return ds;
			}
	};
}

#endif /* _INCLUDE__ROM_SESSION__CLIENT_H_ */
